<html>
<head><meta charset="utf-8"><title>C struct alignment · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html">C struct alignment</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="181006824"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181006824" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181006824">(Nov 18 2019 at 11:18)</a>:</h4>
<p>Hi, i'm not sure this is the best place to ask, but for FFI does alignment <em>has</em> to match or does it has to be rust_align &gt;= c_align?</p>
<p>I'm trying to figure out if something like this can be valid: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=d8f194cd0627fc5b879580de8afb853b" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=d8f194cd0627fc5b879580de8afb853b">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=d8f194cd0627fc5b879580de8afb853b</a><br>
and if it is what if I remove the transmute and make it implicit? (i.e. declaring the c_functions as always accepting <code>C</code> instead of <code>A</code> or <code>B</code>and then not transmuting)</p>



<a name="181011733"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181011733" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181011733">(Nov 18 2019 at 12:37)</a>:</h4>
<p>transmute ignores alignment and only cares about size. If you remove the transmute, I think it's still fine, at least in that direction. Note that in the other direction (taking a C paramter in a function called from C) you need to make sure that the alignment on the Rust side is &lt;= the alignment on the C side.</p>



<a name="181049070"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181049070" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181049070">(Nov 18 2019 at 19:45)</a>:</h4>
<p>the reason transmute can ignore alignment is that it does a <em>copy</em> of the data</p>



<a name="181049084"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181049084" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181049084">(Nov 18 2019 at 19:45)</a>:</h4>
<p>alignment is relevant when talking about <em>pointers to</em> data, not about things passed by-value</p>



<a name="181055710"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181055710" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181055710">(Nov 18 2019 at 21:04)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> You're right. my mistake this is a better example of what I mean: <a href="https://play.rust-lang.org/?gist=f19f7904fae3436a143a7fcaeb755f6a" target="_blank" title="https://play.rust-lang.org/?gist=f19f7904fae3436a143a7fcaeb755f6a">https://play.rust-lang.org/?gist=f19f7904fae3436a143a7fcaeb755f6a</a> (though I'm also interested if transmuting can change the answer here somehow)</p>



<a name="181097479"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181097479" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181097479">(Nov 19 2019 at 10:14)</a>:</h4>
<p>generally the alignment requirement is an "at least requirement"</p>



<a name="181097484"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181097484" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181097484">(Nov 19 2019 at 10:14)</a>:</h4>
<p>so having a 16-aligned pointer to something 4-aligned is perfectly fine</p>



<a name="181097496"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181097496" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181097496">(Nov 19 2019 at 10:14)</a>:</h4>
<p>however, there is one case where you have to be careful, and that is allocators</p>



<a name="181097517"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181097517" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181097517">(Nov 19 2019 at 10:15)</a>:</h4>
<p>allocating memory in rust demands a certain size and alignment; deallocation needs to happen with <em>the same</em> size and alignment</p>



<a name="181097535"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181097535" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181097535">(Nov 19 2019 at 10:15)</a>:</h4>
<p>that's why you cannot transmute a <code>Vec&lt;u32&gt;</code> into a <code>Vec&lt;u8&gt;</code> (of 4 times the length) -- you can make the size match, but not the alignment</p>



<a name="181103822"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181103822" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181103822">(Nov 19 2019 at 11:46)</a>:</h4>
<blockquote>
<p>deallocation needs to happen with the same size and alignment</p>
</blockquote>
<p>This isn't fully accurate, you can deallocate with a different size, and a different alignment - otherwise the actual allocation size returned by <code>alloc_excess</code> would be useless.</p>



<a name="181103879"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181103879" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181103879">(Nov 19 2019 at 11:47)</a>:</h4>
<p>The problem with the <code>Vec</code> example, is that the alignment used when deallocating a <code>Vec&lt;u8&gt;</code> is less than the one used by <code>Vec&lt;u32&gt;</code></p>



<a name="181103948"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181103948" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181103948">(Nov 19 2019 at 11:48)</a>:</h4>
<p>but one can construct a <code>MyVec</code> type for which transmuting <code>MyVec&lt;u32&gt;</code> to <code>MyVec&lt;u8&gt;</code> is fine</p>



<a name="181104031"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181104031" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181104031">(Nov 19 2019 at 11:50)</a>:</h4>
<p>by computing the alignment passed on the deallocation to the allocator from the pointer, instead of just using <code>mem::align_of::&lt;T&gt;()</code></p>



<a name="181104927"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181104927" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181104927">(Nov 19 2019 at 12:04)</a>:</h4>
<p>as long as the <code>Layout</code> matches the allocation class used by the allocator, you are fine</p>



<a name="181123777"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181123777" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181123777">(Nov 19 2019 at 15:46)</a>:</h4>
<blockquote>
<blockquote>
<p>deallocation needs to happen with the same size and alignment</p>
</blockquote>
<p>This isn't fully accurate, you can deallocate with a different size, and a different alignment - otherwise the actual allocation size returned by <code>alloc_excess</code> would be useless.</p>
</blockquote>
<p>uh, what? the docs say quite clearly that the size+align must match?</p>



<a name="181123824"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181123824" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181123824">(Nov 19 2019 at 15:46)</a>:</h4>
<blockquote>
<p>as long as the <code>Layout</code> matches the allocation class used by the allocator, you are fine</p>
</blockquote>
<p>that's an allocator-internal thing. for allocator-agnostic code, you have to assume that the allocator has one class per size+alloc combination.</p>



<a name="181123867"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181123867" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181123867">(Nov 19 2019 at 15:47)</a>:</h4>
<blockquote>
<p>by computing the alignment passed on the deallocation to the allocator from the pointer, instead of just using <code>mem::align_of::&lt;T&gt;()</code></p>
</blockquote>
<p>I dont think you can compute the alignment from the pointer, without making assumptions beyond what the allocator API provides?</p>



<a name="181134035"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181134035" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181134035">(Nov 19 2019 at 17:23)</a>:</h4>
<blockquote>
<p>uh, what? the docs say quite clearly that the size+align must match?</p>
</blockquote>
<p>Then the docs are wrong, because you can call <code>Alloc::alloc_excess</code> and use the size returned by it, which is often larger than the size requested, for deallocation.</p>
<p>If some docs claim that you can only use the size requested, then those docs are wrong. There is at least a range of sizes that you can use, and that range is at least <code>[requested, actual_size)</code>.</p>



<a name="181134094"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181134094" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181134094">(Nov 19 2019 at 17:24)</a>:</h4>
<blockquote>
<p>I dont think you can compute the alignment from the pointer,</p>
</blockquote>
<p>You can find the largest power of two for which addr % pow2 == 0.</p>



<a name="181134736"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181134736" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181134736">(Nov 19 2019 at 17:30)</a>:</h4>
<p>AFAICT the docs do not say that anywhere, e.g., <a href="https://doc.rust-lang.org/beta/std/alloc/trait.Alloc.html" target="_blank" title="https://doc.rust-lang.org/beta/std/alloc/trait.Alloc.html">std::alloc::Alloc</a> docs explicitly say:</p>
<blockquote>
<p>if a layout <code>k</code> fits a memory block (denoted by <code>ptr</code>) currently allocated via an allocator <code>a</code>, then it is legal to use that layout to deallocate it, i.e., <code>a.dealloc(ptr, k);</code></p>
</blockquote>



<a name="181134808"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181134808" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181134808">(Nov 19 2019 at 17:30)</a>:</h4>
<p>The only requirement is that the <code>Layout</code> used for deallocation must fit the actual <code>Layout</code> of the allocation</p>



<a name="181134849"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181134849" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181134849">(Nov 19 2019 at 17:31)</a>:</h4>
<p>The actual <code>Layout</code> of the allocation can be different than the <code>Layout</code> requested, and can also be different than the <code>Layout</code> returned by the <code>Alloc</code> trait methods</p>



<a name="181134882"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181134882" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181134882">(Nov 19 2019 at 17:31)</a>:</h4>
<p>But the <code>Layout</code> requested and the <code>Layout</code>s returned must fit the actual <code>Layout</code>, because you can use either during deallocation.</p>



<a name="181135016"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181135016" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181135016">(Nov 19 2019 at 17:33)</a>:</h4>
<p>Those docs expand on what "fit" means.</p>



<a name="181135034"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181135034" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181135034">(Nov 19 2019 at 17:33)</a>:</h4>
<p>And in particular for aligned, it says:</p>
<blockquote>
<p>The block's starting address must be aligned to <code>layout.align()</code>.</p>
</blockquote>



<a name="181135312"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181135312" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181135312">(Nov 19 2019 at 17:36)</a>:</h4>
<p>Ah wait, <a href="https://doc.rust-lang.org/beta/std/alloc/trait.Alloc.html#tymethod.dealloc" target="_blank" title="https://doc.rust-lang.org/beta/std/alloc/trait.Alloc.html#tymethod.dealloc"><code>Alloc::dealloc</code></a> imposes an extra requirement: </p>
<blockquote>
<p>In addition to fitting the block of memory layout, the alignment of the layout must match the alignment used to allocate that block of memory.</p>
</blockquote>



<a name="181135330"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181135330" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181135330">(Nov 19 2019 at 17:36)</a>:</h4>
<p>So the size only needs to "fit", but the alignment needs to exactly match.</p>



<a name="181140823"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181140823" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181140823">(Nov 19 2019 at 18:36)</a>:</h4>
<blockquote>
<blockquote>
<p>I dont think you can compute the alignment from the pointer,</p>
</blockquote>
<p>You can find the largest power of two for which addr % pow2 == 0.</p>
</blockquote>
<p>that has no correlation with the alignment requested though, so it is useless</p>



<a name="181289256"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181289256" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181289256">(Nov 21 2019 at 09:51)</a>:</h4>
<p>Depends on the allocator</p>



<a name="181289366"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181289366" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181289366">(Nov 21 2019 at 09:52)</a>:</h4>
<p>If your allocator supports deallocating with any alignment that’s satisfied by a pointer, then you can always pass just 1, but the generic API doesn’t require all allocators to support that.</p>



<a name="181289406"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181289406" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181289406">(Nov 21 2019 at 09:53)</a>:</h4>
<p>A specific impl can support that</p>



<a name="181292163"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181292163" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181292163">(Nov 21 2019 at 10:32)</a>:</h4>
<blockquote>
<p>Depends on the allocator</p>
</blockquote>
<p>all of this discussion has been for "normal" Rust code. as in, the only thing you know about the allocator is what the allocator API explicitly says. quite clearly that is the default and anyone asking here "may I do this" doesn't mean "does there exist an allocator for which I may do this".<br>
I dont understand why you insist on bringing in guarantees only made by certain allocators when that is irrelevant for the discussion.</p>



<a name="181301439"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181301439" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181301439">(Nov 21 2019 at 12:57)</a>:</h4>
<p>You claimed that the size and alignment must exactly match</p>



<a name="181301448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181301448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181301448">(Nov 21 2019 at 12:57)</a>:</h4>
<p>I hope I have convinced you that this claim is wrong</p>



<a name="181301458"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181301458" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181301458">(Nov 21 2019 at 12:57)</a>:</h4>
<p>Only the alignment must match</p>



<a name="181301570"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181301570" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181301570">(Nov 21 2019 at 12:58)</a>:</h4>
<p>The allocator api is derived from real allocators, <del>I don't know any for which the alignment must actually match</del> EDIT: this is the case for jemalloc actually, the alignment must exactly match</p>



<a name="181301618"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181301618" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181301618">(Nov 21 2019 at 12:59)</a>:</h4>
<p>Note that you quoted the unstable Alloc trait, GlobalAlloc::dealloc explicitly says "<code>layout</code> must be the same layout that was used to allocate that block of memory"</p>



<a name="181301748"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181301748" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181301748">(Nov 21 2019 at 13:00)</a>:</h4>
<p>If that's the case, the <code>Alloc</code> trait implementations for <code>GlobalAlloc</code> are unsound</p>



<a name="181301820"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181301820" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181301820">(Nov 21 2019 at 13:02)</a>:</h4>
<p>¯\_(ツ)_/¯</p>



<a name="181301909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181301909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181301909">(Nov 21 2019 at 13:02)</a>:</h4>
<p>and rustc violates that requirement</p>



<a name="181301976"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181301976" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181301976">(Nov 21 2019 at 13:03)</a>:</h4>
<p>The easy fix, naturally, is to drop <code>alloc_excess</code> and change the unstable trait to match the requirements of GlobalAlloc</p>



<a name="181302034"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181302034" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181302034">(Nov 21 2019 at 13:04)</a>:</h4>
<p>The last allocator-wg proposal drops <code>alloc_excess</code>, but it makes <code>alloc</code> return the excess</p>



<a name="181302044"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181302044" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181302044">(Nov 21 2019 at 13:04)</a>:</h4>
<p>meaning there is no <code>GlobalAlloc</code> compatible way to implement <code>Alloc</code></p>



<a name="181302079"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181302079" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181302079">(Nov 21 2019 at 13:05)</a>:</h4>
<p>Well, have fun sorting that mess out, I'm not gonna touch that <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span></p>



<a name="181302128"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181302128" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181302128">(Nov 21 2019 at 13:06)</a>:</h4>
<p>one can probably just require that impls of <code>Alloc</code> that use <code>GlobalAlloc</code> to drop the excess internally, but... i don't think any impl does that</p>



<a name="181302173"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181302173" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181302173">(Nov 21 2019 at 13:06)</a>:</h4>
<p>kind of defeats the point</p>



<a name="181302217"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181302217" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181302217">(Nov 21 2019 at 13:06)</a>:</h4>
<p>there is also a proposal to deprecate <code>GlobalAlloc</code> and replace it with <code>Alloc</code>, which would sort this out as well</p>



<a name="181302332"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181302332" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181302332">(Nov 21 2019 at 13:08)</a>:</h4>
<p>IIUC that would cause the same problem as liberalizing the safety requirement on GlobalAlloc::dealloc, it causes previously valid implementations to become unsound</p>



<a name="181302422"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181302422" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181302422">(Nov 21 2019 at 13:09)</a>:</h4>
<p>Not really, you can only have one <code>#[global_allocator]</code> in a binary, and it has to implement either <code>Alloc</code> or <code>GlobalAlloc</code>. If it implements <code>GlobalAlloc</code>, and <code>Alloc</code> impl shim gets added</p>



<a name="181302486"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181302486" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181302486">(Nov 21 2019 at 13:10)</a>:</h4>
<p>That <code>Alloc</code> shim would need to be generated in such a way that it is sound, but it can just return the requested size as the excess.</p>



<a name="181302498"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181302498" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181302498">(Nov 21 2019 at 13:10)</a>:</h4>
<p>meaning users can only dealloc with the requested size</p>



<a name="181302504"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181302504" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181302504">(Nov 21 2019 at 13:10)</a>:</h4>
<p>I guess?</p>



<a name="181302515"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181302515" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181302515">(Nov 21 2019 at 13:11)</a>:</h4>
<p>Anyway I should stick to my word and not get nerdsniped by this</p>



<a name="181308850"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181308850" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181308850">(Nov 21 2019 at 14:26)</a>:</h4>
<p>This has a much simpler fix.</p>



<a name="181308943"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181308943" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181308943">(Nov 21 2019 at 14:28)</a>:</h4>
<p>Currently there are no <code>GlobalAlloc</code> APIs that return the allocation actual size. Those APIs can be added as trait methods with a default implementation, that just returns the requested size. This allows relaxing Layout constraints of  <code>GlobalAlloc::dealloc</code> to match those of the <code>Alloc</code> trait.</p>



<a name="181308979"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181308979" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181308979">(Nov 21 2019 at 14:28)</a>:</h4>
<p>Current implementations remain sound, because they do not currently provide an implementation of the default trait method.</p>



<a name="181309057"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181309057" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181309057">(Nov 21 2019 at 14:29)</a>:</h4>
<p>If a current implementation were to be modified to provide an impl of the <code>_excess</code> methods  to return the actual allocation size, then this would require updating <code>GlobalAlloc::dealloc</code> to be able to handle that. But doing this would be entirely opt-in.</p>



<a name="181651645"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181651645" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181651645">(Nov 22 2019 at 16:08)</a>:</h4>
<blockquote>
<p>I hope I have convinced you that this claim is wrong</p>
</blockquote>
<p>fair. but you also claimed things about alignment that were wrong (at least if they were meant as a response to my question -- and if not one wonders why they were posted in this thread)... no need to continue discussing that but I feet communication was quite suboptimal here.</p>



<a name="181651708"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181651708" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181651708">(Nov 22 2019 at 16:08)</a>:</h4>
<p>if you think the docs of one of the involved traits are wrong, make a PR?</p>



<a name="181817833"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181817833" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181817833">(Nov 25 2019 at 12:35)</a>:</h4>
<p>The docs of both traits look correct to me, the <code>Alloc</code> trait docs support deallocating with different layouts</p>



<a name="181817898"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181817898" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181817898">(Nov 25 2019 at 12:36)</a>:</h4>
<p>The <code>GlobalAlloc</code> trait does not, but it does not have any APIs for which doing that would make sense</p>



<a name="181818094"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181818094" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181818094">(Nov 25 2019 at 12:38)</a>:</h4>
<p>The current state of affairs just means that you cannot mix-match <code>Alloc</code> and <code>GlobalAlloc</code> calls for the same allocator, e.g., alloc with <code>Alloc::alloc</code> and dealloc wit h<code>GlobalAlloc::dealloc</code>, but that's documented already in the part that talks about "the pointer returned must have been produced by calling one of the <code>Self</code> methods.</p>



<a name="181818333"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181818333" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181818333">(Nov 25 2019 at 12:41)</a>:</h4>
<p>The trait implementation of <code>Alloc</code> and <code>GlobalAlloc</code> themselves can rely on extra semantic guarantees, e.g., have <code>Alloc::alloc</code> do something different than <code>GlobalAlloc::alloc</code> but have <code>Alloc::dealloc</code> just call <code>GlobalAlloc::dealloc</code>.</p>



<a name="181818480"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C%20struct%20alignment/near/181818480" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/C.20struct.20alignment.html#181818480">(Nov 25 2019 at 12:43)</a>:</h4>
<p>I.e. the implementor of the <code>Alloc</code> and <code>GlobalAlloc</code> traits can violate their generic API.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>